/*
 * Copyright (c) 2020 pig4cloud Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.liruo.treasureattic.config.common.jackson;

import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.PackageVersion;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;

import java.io.Serial;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * @see com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
 * 默认使用字符串，不使用timestamp
 * LocalDateTime pattern or DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss"
 * LocalDate DateTimeFormatter.ISO_LOCAL_DATE
 * LocalTime DateTimeFormatter.ISO_LOCAL_TIME
 * Instant useTimestamp ? DateTimeFormatter.ISO_INSTANT : pattern
 */
public class JavaTimeModule extends SimpleModule {
	@Serial
	private static final long serialVersionUID = 7463790583614564159L;

	public JavaTimeModule() {
		this(DEFAULT_PATTERN);
	}
	public JavaTimeModule(Locale locale, ZoneId zoneId) {
		this(DEFAULT_PATTERN, locale, zoneId);
	}
	public JavaTimeModule(String pattern) {
		this(pattern, Locale.getDefault(Locale.Category.FORMAT), ZoneId.systemDefault());
	}
	public JavaTimeModule(String pattern,  Locale locale, ZoneId zoneId) {
		super(PackageVersion.VERSION);
		this.pattern = pattern;
		this.dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, locale).withZone(zoneId);

		// ======================= 时间序列化规则 ===============================
		// yyyy-MM-dd HH:mm:ss
		this.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(this.dateTimeFormatter));
		// yyyy-MM-dd
		this.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE));
		// HH:mm:ss
		this.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ISO_LOCAL_TIME));
		// yyyy-MM-dd HH:mm:ss
		this.addSerializer(Instant.class, new InstantSerializer(InstantSerializer.INSTANCE, this.dateTimeFormatter));

		// ======================= 时间反序列化规则 ==============================
		// yyyy-MM-dd HH:mm:ss
		this.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(dateTimeFormatter));
		// yyyy-MM-dd
		this.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE));
		// HH:mm:ss
		this.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ISO_LOCAL_TIME));
		// yyyy-MM-dd HH:mm:ss
		this.addDeserializer(Instant.class, new InstantDeserializer(InstantDeserializer.INSTANT, this.dateTimeFormatter));
	}

	private final String pattern;
	private final DateTimeFormatter dateTimeFormatter;

	public DateTimeFormatter getDateTimeFormatter() {
		return dateTimeFormatter;
	}

	public String getPattern() {
		return pattern;
	}

	public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

}
